<?php

namespace App\Console\Commands;

use App\Models\DateCollection;
use App\Models\FlowCollection;
use App\Models\ParkInfo;
use Illuminate\Console\Command;
use Carbon\Carbon;
use DB;

class SendMsgCollection extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'msg:send';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '告警统计';

    protected $mobile;

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
        $this->mobile = explode(",",env('MOBILE_MSG_PHONE'));
    }

    /*
    *  发送短信
    * @exp   $content = $route_res->r_username.",".$route_res->r_user_phone.",".date("Y-m-d H:i:s",$route_res->r_order_start_time).",".$data['start_place']."--".$data['end_place'].",". $route_res->r_car_back_place;
    * @param $tpl string m_driver_rec,m_driver_confirm
    */
    function sendMsg($phone, $arr, $tpl){
        if(empty($phone)) return;
        $cont = implode(',', $arr);
        try{
            //手机短信
            $service = app('mobilemsg');
            if(is_array($phone)){
                foreach ($phone as $p) {
                    $resp = $service->send($p, $cont, $tpl);
                }
            } else {
                $resp = $service->send($phone, $cont, $tpl);
            }
        }catch (\Exception $ex){
            return response()->json([
                's' => 0,
                'm' => $ex->getMessage()
            ]);
        }
    }

    /**
     * 获取查询字段
     *
     * @return mixed
     */
    private function queryField()
    {
        $carType = config('custom.pay_type');

        $field = collect($carType)->map(function ($item, $key) {
            $type = $key . '_count';
            return \DB::raw('IFNULL(SUM(' . $type . '),0) as ' . $type);
        });

        $field = $field->merge([
            \DB::raw('IFNULL(SUM(pay_amount),0) as pay_amount'),
        ]);

        return $field->toArray();
    }


    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $start = date("Y-m-d H:i:s",Carbon::yesterday()->timestamp);
        $end = date("Y-m-d H:i:s",Carbon::today()->timestamp);
        $now_day = date("Y-m-d",Carbon::yesterday()->timestamp);

        $parks = ParkInfo::pluck("park_code")->toArray();
        //出场流量汇总
        $field = $this->queryField();
        $charge = DateCollection::whereIn('park_code', $parks)
            ->where('hour', '>=', $start)
            ->where('hour', '<', $end)
            ->first($field)
            ->toArray();
        $pay_amount = $charge['pay_amount'];

        //出场流量汇总
        $leaveFlow = FlowCollection::whereIn('park_code', $parks)
            ->where('hour', '>=', $start)
            ->where('hour', '<=', $end)
            ->sum('leave_flow');
//        echo $pay_amount.'------'.$leaveFlow; exit;
        $this->sendMsg($this->mobile,[$now_day."日",($pay_amount/100)."元",(int)$leaveFlow],"remind_money");
        $this->info('收入短信发送完毕');
    }

}
